#!/usr/bin/perl

use strict 'vars';
package main;

use Getopt::Std;
use Cwd;

use vars qw($opt_d $opt_u $opt_p);

#get command line options
getopts('d:u:p:');
if ($opt_d eq "" || $opt_p eq "") {
    die "Usage: kill-i3-proc.pl -d <conf_dir> -u <username> -p <path>\n";
}
if ($opt_u eq "") {
    $opt_u = "ucb6";
}

chdir ($opt_d) || die "Cannot change to conf directory\n";
opendir (CONF_DIR, ".") || die "Cannot open conf directory\n";

my $filename, my @arr, my $cmd;
while ($filename = readdir(CONF_DIR)) {
    if ($filename =~ "cfg") {
	@arr = split(/_/, $filename);
	print "Host: $arr[1] at port $arr[2]\n";
	$cmd = "ssh $opt_u\@$arr[1] \"cd $opt_p; ./kill-i3-server-on-port.pl -p $arr[2]\"";
	system($cmd);
    }
}
